package com.imut.oa.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.imut.oa.common.PageResult;
import com.imut.oa.pojo.dto.CommonUserPageQueryDTO;
import com.imut.oa.pojo.entity.Leave;
import com.imut.oa.pojo.vo.LeaveVO;
import com.imut.oa.util.ThreadLoaclUtil;
import com.imut.oa.mapper.ExpenseMapper;
import com.imut.oa.pojo.dto.ExpensePageQueryDTO;
import com.imut.oa.pojo.dto.ExpenseSubmitDTO;
import com.imut.oa.pojo.dto.ExpenseUpdateStatusDTO;
import com.imut.oa.pojo.entity.Expense;
import com.imut.oa.pojo.vo.ExpenseVO;
import com.imut.oa.service.ExpenseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.time.LocalDateTime;

@Service
@Slf4j
public class ExpenseServiceImpl extends ServiceImpl<ExpenseMapper, Expense> implements ExpenseService {
    @Autowired
    private ExpenseMapper expenseMapper;
    @Override
    public PageResult page(ExpensePageQueryDTO expensePageQueryDTO) {
        //创建条件对象，不设置任何条件，相当于查询所以
        Page<ExpenseVO> page = new Page<>(expensePageQueryDTO.getPage(), expensePageQueryDTO.getPageSize());
        // 调用自定义的联表分页查询
        Page<ExpenseVO> voPage = expenseMapper.selectExpensePage(page, expensePageQueryDTO);
        return new PageResult(voPage.getTotal(), voPage.getRecords());
    }

    @Override
    public void updateStatus(ExpenseUpdateStatusDTO dto) {
        Expense build = Expense.builder()
                .id(dto.getId())
                .status(dto.getStatus())
                .remark(dto.getRemark())
                .build();
        expenseMapper.updateExpense(build);
    }

    @Override
    public int deleteById(Long id) {
        return baseMapper.deleteById(id);
    }

    @Override
    @Transactional
    public void submitExpense(ExpenseSubmitDTO submitDTO) {
        Long currentId = ThreadLoaclUtil.getCurrentId();
        log.info("获取当前登录用户信息：{}",currentId);
        if (submitDTO == null) {
            throw new RuntimeException("报销信息不能为空");
        }
        Expense expense = new Expense();
        // 这里需要获取当前登录用户ID
        expense.setEmployeeId(currentId);
        expense.setExpenseType(submitDTO.getExpenseType());
        expense.setSubject(submitDTO.getSubject());
        expense.setAmount(submitDTO.getAmount());
        expense.setApplyDocuments(submitDTO.getApplyDocuments());
        expense.setSubmitTime(submitDTO.getSubmitTime());
        expense.setStatus(0);
        expense.setIsDelete(0);
        expenseMapper.insertExpense(expense);
    }

    @Override
    public PageResult getExpenseHistory(CommonUserPageQueryDTO commonUserPageQueryDTO) {
        Page<ExpenseVO> page = new Page<>(commonUserPageQueryDTO.getPage(), commonUserPageQueryDTO.getPageSize());
        Page<ExpenseVO> voPage = expenseMapper.selectUserExpensePage(page, commonUserPageQueryDTO);
        return new PageResult(voPage.getTotal(), voPage.getRecords());
    }

    @Override
    public void cancelLeave(Long expenseId) {
        Expense expense = new Expense();
        expense.setId(expenseId);
        expense.setStatus(3); // 3 表示已撤销
        expenseMapper.updateExpense(expense);
    }
}